package controller

import (
	"github.com/gin-gonic/gin"
	"xs.jsjb/internal/app/user/entity"
	"xs.jsjb/internal/pkg/constant/e"
	"xs.jsjb/internal/pkg/ginx"
)

// Login godoc
// @Summary 测试登录接口
// @Description 测试接口
// @Tags 标签
// @ID /api/user/login
// @Accept  json
// @Produce  json
// @Param body body entity.LoginReq true "body"
// @Success 200 {string} string "ok" "登陆成功"
// @Router /api/user/login [post]
func (u *UserController) Login(c *gin.Context) {
	var (
		err       error
		signParam entity.LoginReq
		token     string
	)
	if errStr := ginx.BindAndValid(c, &signParam); errStr != "" {
		ginx.RespErrorWithMsg(c, e.CodeError, errStr)
		return
	}
	//if token, err = u.userService.Login(c.Request.Context(), &signParam); err != nil {
	//	if errors.Is(err, e.ErrUserNotExist) {
	//		ginx.RespError(c, e.CodeUserNotExist)
	//		return
	//	}
	//	ginx.RespError(c, e.CodeWrongUserNameOrPassword)
	//	return
	//}
	token, err = u.userService.Login(c.Request.Context(), &signParam)

	switch err {
	case nil:
		ginx.RespSuccess(c, token)
	case e.ErrUserNotExist:
		ginx.RespError(c, e.CodeUserNotExist)
	default:
		ginx.RespError(c, e.CodeWrongUserNameOrPassword)
	}
}
